package me.angrybyte.numberpicker.demo;

import me.angrybyte.numberpicker.listener.OnValueChangeListener;
import me.angrybyte.numberpicker.view.ActualNumberPicker;
import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.app.Context;
import org.junit.Before;
import org.junit.Test;

import java.util.Optional;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

public class ActualNumberPickerTest {
    private ActualNumberPicker actualNumberPicker;
    private Context mContext;

    private boolean isValueChanged = false;

    private OnValueChangeListener listener = new OnValueChangeListener() {
        @Override
        public void onValueChanged(int oldValue, int newValue) {
            isValueChanged = true;
        }
    };

    @Before
    public void setUp() {
        mContext = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        actualNumberPicker = new ActualNumberPicker(mContext, new AttrSet() {
            @Override
            public Optional<String> getStyle() {
                return Optional.empty();
            }

            @Override
            public int getLength() {
                return 0;
            }

            @Override
            public Optional<Attr> getAttr(int i) {
                return Optional.empty();
            }

            @Override
            public Optional<Attr> getAttr(String s) {
                return Optional.empty();
            }
        });
    }

    @Test
    public void testBundleName() {
        final String actualBundleName = AbilityDelegatorRegistry.getArguments().getTestBundleName();
        assertEquals("me.angrybyte.numberpicker.demo", actualBundleName);
    }

    @Test
    public void testActualNumberPickerMaxValue() {
        assertEquals(1000, actualNumberPicker.getMaxValue());
        actualNumberPicker.setMaxValue(50);
        assertEquals(50, actualNumberPicker.getMaxValue());
    }

    @Test
    public void testActualNumberPickerMinValue() {
        assertEquals(0, actualNumberPicker.getMinValue());
        actualNumberPicker.setMinValue(50);
        assertEquals(50, actualNumberPicker.getMinValue());
    }

    @Test
    public void testActualNumberPickerValue() {
        assertEquals(500, actualNumberPicker.getValue());
        actualNumberPicker.setValue(20);
        assertEquals(20, actualNumberPicker.getValue());
    }

    @Test
    public void testActualNumberPickerValueListener() throws InterruptedException {
        assertFalse(isValueChanged);
        actualNumberPicker.setListener(listener);
        actualNumberPicker.setValue(20);
        Thread.sleep(1000);
        assertTrue(isValueChanged);
        assertEquals(20, actualNumberPicker.getValue());
    }
}